package com.lhy.mymall.controller;

import com.github.pagehelper.PageInfo;
import com.google.zxing.WriterException;
import com.lhy.mymall.common.ApiRestResponse;
import com.lhy.mymall.exception.MallException;
import com.lhy.mymall.request.CreateOrderReq;
import com.lhy.mymall.service.OrderService;
import com.lhy.mymall.vo.OrderVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;

/**
 * @author: 卢辉宇
 * @date: 2022/12/19 15:47
 * @description: 订单控制类
 */
@RestController
public class OrderController {

    @Resource
    OrderService orderService;

    /**
     * 创建订单
     * @param createOrderReq
     * @return
     * @throws MallException
     */
    @PostMapping("/order/create")
    public ApiRestResponse create(@Valid @RequestBody CreateOrderReq createOrderReq) throws MallException {
        //service层完成创建订单的逻辑。。。
        String orderNo = orderService.create(createOrderReq);
        return ApiRestResponse.success(orderNo);
    }

    /**
     * 订单详情
     * @param orderNo 订单号
     * @return
     */
    @PostMapping("/order/detail")
     public ApiRestResponse detail(String orderNo) throws MallException {
         //调用service层的详情方法。。。
        OrderVO detail = orderService.detail(orderNo);
        return ApiRestResponse.success(detail);
     }

    /**
     * 用户订单列表
     * @param pageNum
     * @param pageSize
     * @return
     * @throws MallException
     */
     @PostMapping("/order/list")
     public ApiRestResponse list(Integer pageNum,Integer pageSize) throws MallException {
        //调用service层用户列表方法。。。
         PageInfo pageInfo=orderService.listForCustomer(pageNum,pageSize);

        return ApiRestResponse.success(pageInfo);
     }

     /**
     * 取消订单
     * @param orderNo 订单号
     * @return
     */
    @PostMapping("/order/cancel")
     public ApiRestResponse cancel(String orderNo) throws MallException {
         //调用service层的详情方法。。。
        orderService.cancel(orderNo);
        return ApiRestResponse.success();
     }

    /**
     * 生成支付二维码
     * @param orderNo
     * @return
     */
     @PostMapping("/order/qrcode")
     public ApiRestResponse qrcode(String orderNo) throws IOException, WriterException, MallException {
         //调用service层的生成支付二维码方法。。。
         String qrcode=orderService.qrcode(orderNo);

         return ApiRestResponse.success(qrcode);
     }

    /**
     * 支付接口
     * @param orderNo
     * @return
     */
    @GetMapping("/pay")
     public ApiRestResponse pay(String orderNo) throws MallException {
         //调用service的支付方法。。。
        orderService.pay(orderNo);

        return ApiRestResponse.success();
     }

    /**
     * 订单完结
     * @param orderNo
     * @return
     * @throws MallException
     */
     @PostMapping("/order/finish")
     public ApiRestResponse finish(String orderNo) throws MallException {
        //调用service层的finish方法。。。
         orderService.finish(orderNo);

        return ApiRestResponse.success();
     }

}
